package com.lyw.guide

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import coil.load
import com.blankj.utilcode.util.LogUtils
import com.lu.library.Constants
import com.lu.library.base.BaseActivity
import com.lu.library.ext.navigation
import com.lyw.guide.databinding.ActivityGuideBinding
import com.lyw.login_api.LoginRoutePaths
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch

class GuideActivity:BaseActivity<ActivityGuideBinding>() {


    private val viewModel by viewModels<GuideViewModel>()


    private  var bannerImageAdapter = object:BannerImageAdapter<String>(null){
        override fun onBindView(holder: BannerImageHolder, p1: String, position: Int, size: Int) {
            LogUtils.d("p1:$p1")
            holder.imageView.load(p1)
        }

    }
    override fun initViews(){
        binding.banner
            .addBannerLifecycleObserver(this)
            .setAdapter(bannerImageAdapter)
            .setIndicator(CircleIndicator(this))
    }

    override fun initListener() {
        binding.banner.setOnBannerListener { any, i ->
            LogUtils.d("i:$i")
            if (bannerImageAdapter.itemCount-1 == i) {
//                startActivityAndSelfFinish(LoginActivity::class.java)
                LoginRoutePaths.PATH_LOGIN.navigation()
                finish()
            }
        }
    }
    override fun initData() {
       lifecycleScope.launch {
           viewModel.uiStateFlow.collect{
               when(it){
                   GuideState.INIT -> {}
                   is GuideState.SUCCESS -> {
                       LogUtils.d(it.images.toTypedArray().contentToString())
                        bannerImageAdapter.setDatas(it.images)
                   }
               }
           }
       }
        viewModel.sendUiIntent(GuideIntent.GetBanner)

    }

    override fun layoutResId() =  R.layout.activity_guide
}